# -*- coding: utf-8 -*-
# @author okumura@laurus.co.jp

from gaeo.controller import BaseController
from libs.laurus.action import before_action
from google.appengine.ext.webapp.util import login_required
from model.hatomo import Hatomo


class WelcomeController(BaseController):
    def before_action(self):
        before_action(self)

    @login_required
    def index(self):
        self.hatomo = Hatomo.get_by_key_name(self.user.email())

    @login_required
    def regist(self):
        if Hatomo.get_by_key_name(self.user.email()):
            self.redirect('/')
            return

        hatomo = Hatomo(key_name=self.user.email(),user=self.user,name=self.user.nickname())
        hatomo.put()
        self.session['message'] = 'ハトモに登録しました'
        self.session.put()
        self.redirect('/')

    @login_required
    def delete(self):
        hatomo = Hatomo.get_by_key_name(self.user.email())
        if not hatomo:
            self.redirect('/')
            return
        hatomo.delete()
        self.session['message'] = 'ハトモから削除しました'
        self.session.put()
        self.redirect('/')
